<template>
	<view style="margin:10px">
		<u--form :rules="dataRule" ref="uForm" :model="dataForm" labelAlign="left" v-if="type == 1">
			<u-form-item label="原密码:" prop="oldPassword" borderBottom labelWidth="100px">
				<u--input v-model="dataForm.oldPassword" border="none" placeholder="请输入原密码"  type="password"></u--input>
			</u-form-item>
			<u-form-item label="新密码:" prop="newPassword" borderBottom labelWidth="100px">
				<u--input v-model="dataForm.newPassword" border="none" placeholder="请输入新密码"  type="password"></u--input>
			</u-form-item>
			<u-form-item label="确认新密码:" prop="certainPassword" borderBottom labelWidth="100px" >
				<u--input v-model="dataForm.certainPassword" border="none" placeholder="请输入确认新密码"  type="password"></u--input>
			</u-form-item>
			<view style="display: flex;">
				<u-button type="primary" text="确认" @click="submit" style="margin :30px 0 0 10px; width: 100px" />
			</view>
		</u--form>
		<u--form :rules="dataRule2" ref="uForm" :model="dataForm2" labelAlign="left" v-else>
			<u-form-item label="设置密码:" prop="password" borderBottom labelWidth="100px">
				<u--input v-model="dataForm2.password" border="none" placeholder="请输入密码" type="password"></u--input>
			</u-form-item>
			<u-form-item label="确认密码:" prop="certainPassword" borderBottom labelWidth="100px">
				<u--input v-model="dataForm2.certainPassword" border="none" placeholder="请确认密码" type="password"></u--input>
			</u-form-item>
			<view style="display: flex;">
				<u-button type="primary" text="确认" @click="submit2" style="margin :30px 0 0 10px; width: 100px" />
			</view>
		</u--form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataForm: {
					oldPassword: '',
					newPassword: '',
					certainPassword: ''
				},
				dataForm2:{
					password:'',
					certainPassword:''
				},
				dataRule: {
					'oldPassword': {
						required: true,
						message: '请输入原密码'
					},
					'newPassword': {
						min: 3,
						required: true,
						message: '不能少于3位'
					},
					'certainPassword': {
						required: true,
						message: '请确认新密码'
					}
				},
				dataRule2: {
					'password': {
						required: true,
						message: '请输入密码'
					},
					'certainPassword': {
						required: true,
						message: '请确认密码'
					}
				},
				//该用户有密码则为1，否则为2
				type:1
			}
		},
		onLoad(option){
			this.type = option.type
		},
		methods: {
			submit() {
				this.$refs.uForm.validate().then(res => {
					if(this.dataForm.newPassword == this.dataForm.certainPassword){
						this.updatePassword()
					}else{
						uni.$u.toast('两次密码不一致')
					}
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			//设置密码
			submit2(){	
				this.$refs.uForm.validate().then(res => {
					if(this.dataForm2.password == this.dataForm2.certainPassword){
						this.setPassword()
					}else{
						uni.$u.toast('两次密码不一致')
					}
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			async setPassword(){
				const res = await this.$myRequest({
					url: 'user/applet/setPassword',
					method:'POST',
					data:this.dataForm2.password
				})
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			},
			async updatePassword() {
				const res = await this.$myRequest({
					url: 'user/applet/updatePassword',
					method: 'POST',
					data: {
						'oldPassword': this.dataForm.oldPassword,
						'newPassword': this.dataForm.newPassword,
					},
					// 使得以上两个数据传到后端为两个对象而不是一个
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					}
				})
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			}
		}
	}
</script>

<style scoped lang="scss">

</style>
